using System.Collections.Generic;
using System.Text;

namespace OracleInternal.SqlAndPlsqlParser
{
	internal class ParserRuleTuple : RuleTupleBase<int>
	{
		public int m_vBaseHead = -1;

		public int[] m_vBaseRhs;

		protected ParserGrammarDefinition m_vParserGrammar;

		public ParserGrammarDefinition ParserGrammar => m_vParserGrammar;

		static ParserRuleTuple()
		{
			RuleTupleBase<int>.s_vNullHeadHashCode = 0.GetHashCode();
		}

		public ParserRuleTuple(int h, List<int> r, ParserGrammarDefinition parserGrammar)
			: base(h, r)
		{
			m_vParserGrammar = parserGrammar;
			GetBaseSymbols();
		}

		public ParserRuleTuple(int h, int[] r, ParserGrammarDefinition parserGrammar)
			: base(h, r)
		{
			m_vParserGrammar = parserGrammar;
			GetBaseSymbols();
		}

		protected void GetBaseSymbols()
		{
			string text = m_vParserGrammar.m_vAllSymbols[m_vHead];
			int num = text.IndexOf('[');
			if (num >= 0)
			{
				text = text.Substring(0, num);
				m_vBaseHead = m_vParserGrammar.m_vSymbolIndexes[text];
			}
			else
			{
				m_vBaseHead = m_vHead;
			}
			if (m_vRhs == null)
			{
				return;
			}
			int num2 = m_vRhs.Length;
			m_vBaseRhs = new int[num2];
			for (int i = 0; i < num2; i++)
			{
				int num3 = m_vRhs[i];
				if (num3 < 0)
				{
					m_vBaseRhs[i] = num3;
					continue;
				}
				text = m_vParserGrammar.m_vAllSymbols[num3];
				num = text.IndexOf('[');
				if (num >= 0)
				{
					text = text.Substring(0, num);
					m_vBaseRhs[i] = m_vParserGrammar.m_vSymbolIndexes[text];
				}
				else
				{
					m_vBaseRhs[i] = num3;
				}
			}
		}

		public override int CompareTo(RuleTupleBase<int> src)
		{
			ParserRuleTuple parserRuleTuple = src as ParserRuleTuple;
			if (parserRuleTuple == null)
			{
				throw new ParserException(ParserExceptionType.Grammar, ParserExceptionError.MismatchedTuplesComparison);
			}
			if (m_vParserGrammar != parserRuleTuple.m_vParserGrammar)
			{
				throw new ParserException(ParserExceptionType.Grammar, ParserExceptionError.DifferentGrammarsTuples);
			}
			int num = m_vHead - parserRuleTuple.m_vHead;
			if (num == 0)
			{
				int[] vRhs = parserRuleTuple.m_vRhs;
				if (m_vRhs == null)
				{
					if (vRhs != null)
					{
						num = -1;
					}
				}
				else if (vRhs == null)
				{
					num = 1;
				}
				else
				{
					int num2 = m_vRhs.Length;
					num = num2 - vRhs.Length;
					int num3 = 0;
					while (num == 0 && num3 < num2)
					{
						num = m_vRhs[num3] - vRhs[num3];
						num3++;
					}
				}
			}
			return num;
		}

		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder(1024);
			if (m_vHead >= 0)
			{
				string value = m_vParserGrammar.m_vAllSymbols[m_vHead];
				stringBuilder.Append(value);
				stringBuilder.Append(':');
			}
			int[] vRhs = m_vRhs;
			foreach (int num in vRhs)
			{
				stringBuilder.Append(' ');
				if (num >= 0)
				{
					string value = m_vParserGrammar.m_vAllSymbols[num];
					stringBuilder.Append(value);
				}
			}
			return stringBuilder.ToString();
		}

		public virtual string ToString(int pos)
		{
			StringBuilder stringBuilder = new StringBuilder(1024);
			if (m_vHead >= 0)
			{
				string value = m_vParserGrammar.m_vAllSymbols[m_vHead];
				stringBuilder.Append(value);
				stringBuilder.Append(':');
			}
			int num = m_vRhs.Length;
			for (int i = 0; i < num; i++)
			{
				stringBuilder.Append(' ');
				int num2 = m_vRhs[i];
				if (num2 >= 0)
				{
					if (i == pos)
					{
						stringBuilder.Append('!');
					}
					string value = m_vParserGrammar.m_vAllSymbols[num2];
					stringBuilder.Append(value);
				}
			}
			if (num == pos)
			{
				stringBuilder.Append('!');
			}
			stringBuilder.Append(';');
			return stringBuilder.ToString();
		}
	}
}
